﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IData;
using Dapper;
using System.Data;
using System.Data.SqlClient;

using System.Reflection;
using System.Collections;
using System.Configuration;
using System.Reflection;

namespace Data
{
    public class DAL<T> : IData.IDAL<T>
    {
        /// <summary>
        /// 初始化连接
        /// </summary>
        IDbConnection conn = new SqlConnection(ConfigurationManager.AppSettings["conn"].ToString());

        public int AddData(T t, string filter)
        {
            //获取实体属性
            PropertyInfo[] pi = t.GetType().GetProperties();
            //获取实体名称
            string className = t.GetType().Name;
            string sql = string.Format(" insert into {0}(", className);
            string sqltext = " values(";
            foreach (PropertyInfo item in pi)
            {
                //判断该属性是否为过滤字段
                if (item.Name.ToLower() == filter.ToLower())
                {
                    continue;
                }
                //获取属性值
                object value = item.GetValue(t);
                //判断该值是否为空
                if (value != null)
                {
                    //获取属性名称
                    sql += item.Name+",";
                    //判断该属性值是否为int类型
                    if (item.GetValue(t).GetType().IsValueType)
                    {
                        sqltext += value + ",";
                    }
                    else
                    {
                        sqltext+="'"+value+ "',";
                    }
                }
            }
            sql = sql.Substring(0, sql.Length - 1) + ") " + sqltext.Substring(0, sqltext.Length - 1) + ")";
            return conn.Execute(sql);
        }

        public int DelData(T t, string where)
        {
            return conn.Execute(string.Format("delete from {0} where {1}", t.GetType().Name, where));
        }

        public int EditData(T t,string filter, string where)
        {
            string className = t.GetType().Name;
            string sql = string.Format(" update {0} set ",className);
            foreach (PropertyInfo item in t.GetType().GetRuntimeProperties())
            {
                if (item.Name.ToLower() == filter.ToLower())
                {
                    continue;
                }
                if (item.GetValue(t).GetType().IsValueType)
                {
                    sql += item.Name + "=" + item.GetValue(t) + ",";
                }
                else
                {
                    sql += item.Name + "='" + item.GetValue(t) + "',";
                }
            }
            sql = string.Format(sql.Substring(0,sql.Length-1)+" where {0}",where);
            return conn.Execute(sql);
        }

        public List<T> Pager(T t, string where, int pageNo, int page)
        {
            string className = t.GetType().Name;
            string sql = " select {0}  from {1} where {2}";
            string search = string.Empty;
            foreach (PropertyInfo item in t.GetType().GetRuntimeProperties())
            {
                search += item.Name;
            }
            sql = string.Format(search.Substring(0, search.Length - 1), className, where);
            return conn.Query<T>(sql).Skip((pageNo)-1*page).Take(page).ToList();
        }

        public List<T> Query(string sqltext)
        {
            return conn.Query<T>(sqltext).ToList();
        }

        public T Query(T t, string where)
        {
            string className = t.GetType().Name;
            string sql = " select {0}  from {1} where {2}";
            string search = string.Empty;
            foreach (PropertyInfo item in t.GetType().GetRuntimeProperties())
            {
                search += item.Name;
            }
            sql = string.Format(search.Substring(0, search.Length - 1), className, where);
            return conn.Query<T>(sql).FirstOrDefault();
        }

        public List<T> QueryList(T t, string where)
        {
            string className = t.GetType().Name;
            string sql = " select {0}  from {1} where {2}";
            string search = string.Empty;
            foreach (PropertyInfo item in t.GetType().GetRuntimeProperties())
            {
                search += item.Name;
            }
            sql = string.Format(search.Substring(0, search.Length - 1), className, where);
            return conn.Query<T>(sql).ToList();
        }
    }
}
